package org.example;


public class Test1 {
    //leetcode 字符串相乘
    //https://leetcode.cn/problems/multiply-strings/description/
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) return "0";
        int n1 = num1.length(), n2 = num2.length();
        //最终结果的位数就是num1的长度加上num2的长度-1
        //num1*num2的结果ret的第i位就为，num1和num2和为i的数的乘积
        //例如123*456=56088
        //56088的第4位数8，就是3*6%10=8
        //第三位数8，2*6+3*5+1=28%10=8
        int n = n1 + n2 - 1;
        int[] ret = new int[n];
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                ret[i+j] += (num1.charAt(i)-'0') * (num2.charAt(j)-'0');
            }
        }
        //对数组中的数进行整理，整理为最终结果
        StringBuilder sb = new StringBuilder(); //sb记录最终结果
        int tmp = 0; //sum统计最终结果，tmp记录进位
        for (int i = n-1; i >= 0 || tmp != 0; i--) {
            if (i >= 0) tmp += ret[i];
            sb.append(tmp % 10);
            tmp /= 10;
        }
        return sb.reverse().toString();
    }

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        test1.multiply("123","456");
        System.out.println(666);
    }
}
